package game.objects;
import java.util.Vector;

import game.Game;
import game.engine.BodyToGraphics;
import game.engine.GSync;
import game.engine.GSync.SyncType;
import graphics.objects.GStupidLine;
import graphics.objects.GraphicsObject;
import graphics.system.Color;
import physics.Body;
import physics.joints.JDistanceJoint;
import physics.joints.Joint;
import physics.tools.Initializer;
import tools.Floatmath;
import tools.Point;
/**
 * This is just meant to be complex to test if the game-engine actually works correctly.
 * 
 * @author toni
 * 
 */
public class GOComplex extends GameObject {
	public GOComplex(Game g, Point pos) {
		super(g, pos);
	}
	@Override
	public void handle() {
		float RAND = 0.001f;
		if (timeExisting() > 1.0f)
			return;
		if (Math.random() < 20 * RAND)
			addRandomBody();
		if (Math.random() < RAND)
			remRandomBody();
		if (Math.random() < 20 * RAND)
			addRandomJoint();
		if (Math.random() < RAND)
			remRandomJoint();
		if (Math.random() < 0) {
			addRandomGraphicsObject();
			if (Math.random() < 0)
				remRandomGraphicsObject();
		}
	}
	private void remRandomGraphicsObject() {
		if (numGraphicsObjects > 1)
			remGraphicsObject(getGraphicsObject(0));
	}
	private void addRandomGraphicsObject() {
		if (Math.random() < 0.5f) {
			Vector<Body> alreadyDrawn = new Vector<Body>();
			for (int i = 0; i < numBodies; i++) {
				Body b = getBody(i);
				for (int j = 0; j < numGraphicsObjects; j++) {
					GraphicsObject o = getGraphicsObject(j);
					if (getSync(o).type == SyncType.BODY && getSync(o).targetId == b.id) {
						alreadyDrawn.add(b);
					}
				}
			}
			Body toDraw = null;
			for (int i = 0; i < numBodies; i++) {
				if (!(alreadyDrawn.contains(getBody(i)))) {
					toDraw = getBody(i);
				}
			}
			if (toDraw != null) {
				addGraphicsObject(BodyToGraphics.get(toDraw, new Color(Floatmath.random(), Floatmath.random(), Floatmath.random(), 1.0f)), new GSync(toDraw));
			}
		}
		else {
			if (Math.random() < 0.5f) {
				Vector<Joint> alreadyDrawn = new Vector<Joint>();
				for (int i = 0; i < numJoints; i++) {
					Joint b = getJoint(i);
					for (int j = 0; j < numGraphicsObjects; j++) {
						GraphicsObject o = getGraphicsObject(j);
						if (getSync(o).type == SyncType.JOINT && getSync(o).targetId == b.id) {
							alreadyDrawn.add(b);
						}
					}
				}
				Joint toDraw = null;
				for (int i = 0; i < numJoints; i++) {
					if (!(alreadyDrawn.contains(getJoint(i)))) {
						toDraw = getJoint(i);
					}
				}
				if (toDraw != null) {
					JDistanceJoint j = ((JDistanceJoint) toDraw);
					addGraphicsObject(new GStupidLine(j.getP1(), j.getP2(), 0.0f, new Color(0, 0, 0, 1)), new GSync(j));
				}
			}
		}
	}
	private void remRandomJoint() {
		if (numJoints > 0) {
			remJoint(getJoint(0));
		}
	}
	private void addRandomJoint() {
		if (numBodies >= 2) {
			addJoint(Initializer.dJoint(getBody(0), getBody(1)));
		}
	}
	private void addRandomBody() {
		addBodyWithGraphics(Initializer.getRectangle(new Point(Floatmath.random() * 1000, Floatmath.random() * 1000), 30, 30, 1.0f), new Color(0, 0, 0, 1));
	}
	private void remRandomBody() {
		if (numBodies > 0) {
			Body b = getBody(0);
			for (int i = 0; i < numGraphicsObjects; i++) {
				GraphicsObject g = getGraphicsObject(i);
				if (getSync(g).targetId == b.id) {
					remGraphicsObject(g);
				}
			}
			remBody(b);
		}
	}
	@Override
	public void initialize(Point pos) {
	}
}
